/** VARIABLES **/
//current menu id
var currentMenuId = "unset";
//left most end for the menu scroll area
var leftCap;
//right most end for the menu scroll area
var rightCap;
//menu scroll speed
var speed = 50;
//whether menu should move or not
var move = false;

/** EVENT HANDLING **/
var addEvent = function(elem, type, eventHandle) {
    if (elem == null || elem == undefined) return;
    if ( elem.addEventListener ) {
        elem.addEventListener( type, eventHandle, false );
    } else if ( elem.attachEvent ) {
        elem.attachEvent( "on" + type, eventHandle );
    }
};

/** FUNCTIONS **/
//This functions re-paints DOM elements that depend on the window dimensions
function repaint() {
	//initMenubar();
	resizeIframe();
}

//loads a page from within the iframe
function loadPage(url,menuid) {
	document.getElementById("content_frame").src= webRoot + "/php/content_template.php?url=" + url;
	
	if (menuid != null) {
		highlightCurrentMenu(menuid);
	}
}

function loadHome() {
	document.getElementById("content_frame").src= webRoot + "/php/content_template.php?url=./home.php";
	
	if (currentMenuId != 'unset') {
		document.getElementById(currentMenuId).className = "enabled_menu clickable";
	}
	currentMenuId = 'unset';
}

//highlights current menu
function highlightCurrentMenu(menuid) {
	//unload last menu
	if (currentMenuId != 'unset') {
		document.getElementById(currentMenuId).className = "enabled_menu clickable";
	}
	
	//load new menu
	currentMenuId = menuid;
	document.getElementById(menuid).className ="current_menu non-clickable";
}

//show or hide comment entry form
function showHideCommentEntry(icon) {
	var form = document.getElementById("comment-entry");
	if (form.style.display == "none" || form.style.display == "") {
		form.style.display = "inline";
		icon.src = webRoot + "/images/expanded.gif";
		document.getElementById("form-msg").innerHTML = "";
	} else {
		form.style.display = "none";
		icon.src = webRoot + "/images/collapsed.gif";
	}
	parent.resizeIframe(document.body.scrollHeight);
}

var Comment = new Object();
Comment.print = function(dtTime,name,location,message,section,append) {
	var str = name + ", ";
	str+= location + " [";
	str+= dtTime + "]";
	var header = document.createElement("div");
	header.className = "comment-header";
	header.innerHTML = str;
	
	str = message;
	var body = document.createElement("div");
	body.className = "comment-body";
	body.innerHTML = str;
	if(append) {
		section.appendChild(header);
		section.appendChild(body);
	} else {
		section.insertBefore(body,section.childNodes[0]);
		section.insertBefore(header,section.childNodes[0]);
	}
}


function resetGenericPagination(pageNo,totalPages) {
	//reset the nav sections
	var topNav = document.getElementById("nav-top");
	var bottomNav = document.getElementById("nav-bottom");
	topNav.innerHTML = "";
	bottomNav.innerHTML = "";
	//print the last page button
	var elem = null;
	elem = document.createElement("span");
	if (totalPages > pageNo) {
		addEvent(elem,"click", function() { Paginator.go(totalPages); return false;});
		elem.setAttribute("onclick", "Paginator.go(" + totalPages + "); return false;");
		elem.className = "nav-button";
	} else {
		elem.className = "nav-button-inactive";
	}
	elem.appendChild(document.createTextNode(">>"));
	topNav.appendChild(elem);
	bottomNav.appendChild(elem.cloneNode(true));
	//print the next button
	elem = document.createElement("span");
	if (totalPages > pageNo) {
		addEvent(elem,"click", function() { Paginator.go(pageNo + 1); return false;});
		elem.setAttribute("onclick", "Paginator.go(" + (pageNo + 1) + "); return false;");
		elem.className = "nav-button";
	} else {
		elem.className = "nav-button-inactive";
	}
	elem.appendChild(document.createTextNode(">"));
	topNav.appendChild(elem);
	bottomNav.appendChild(elem.cloneNode(true));
	//print the page numbers
	var end;
	if (pageNo == totalPages) end = totalPages;
	else if (pageNo == 1) {
		if (totalPages >= 3) end = 3;
		else end = 2;
	} else {
		end = pageNo + 1;
	}
	var page1 = end - 2;
	var page2 = end - 1;
	var page3 = end;
	if (end < 3) {
		page1 = 1;
	}
	for (var i = page3; i >= page1; i--) {
		elem = document.createElement("span");
		if (i == pageNo) {
			elem.className = "nav-page-no-inactive";
		} else {
			elem.className = "nav-page-no";
			if (i == page1) {
				addEvent(elem,"click", function() { Paginator.go(page1); return false;});
				elem.setAttribute("onclick", "Paginator.go(" + page1 + "); return false;");
			} else if (i == page2) {
				addEvent(elem,"click", function() { Paginator.go(page2); return false;});
				elem.setAttribute("onclick", "Paginator.go(" + page2 + "); return false;");
			} else {
				addEvent(elem,"click", function() { Paginator.go(page3); return false;});
				elem.setAttribute("onclick", "Paginator.go(" + page3 + "); return false;");
			}
		}
		elem.appendChild(document.createTextNode(i));
		topNav.appendChild(elem);
		bottomNav.appendChild(elem.cloneNode(true));
	}
	//print the prev button
	elem = document.createElement("span");
	if (pageNo > 1) {
		addEvent(elem,"click", function() { Paginator.go(pageNo - 1); return false;});
		elem.setAttribute("onclick", "Paginator.go(" + (pageNo - 1) + "); return false;");
		elem.className = "nav-button";
	} else {
		elem.className = "nav-button-inactive";
	}
	elem.appendChild(document.createTextNode("<"));
	topNav.appendChild(elem);
	bottomNav.appendChild(elem.cloneNode(true));
	//print the first page button
	elem = document.createElement("span");
	if (pageNo > 1) {
		addEvent(elem,"click", function() { Paginator.go(1); return false;});
		elem.setAttribute("onclick", "Paginator.go(1); return false;");
		elem.className = "nav-button";
	} else {
		elem.className = "nav-button-inactive";
	}
	elem.appendChild(document.createTextNode("<<"));
	topNav.appendChild(elem);
	bottomNav.appendChild(elem.cloneNode(true));
	//print the page of section
	elem = document.createElement("span");
	elem.innerHTML = "Page " + pageNo + " of " + totalPages;
	topNav.appendChild(elem);
	bottomNav.appendChild(elem.cloneNode(true));

	topNav.style.visibility = "visible";
	bottomNav.style.visibility = "visible";
}

var News = new Object();
News.print = function(dtTime,newsText,url,type,section,divider) {
	var news = document.createElement("div");
	news.className = "news";
	
	var header = document.createElement("span");
	header.className = "news-header";
	header.innerHTML = dtTime + "<br/>";
	news.appendChild(header);
	
	
	news.appendChild(document.createTextNode(newsText));
	if (url != null) {
		news.appendChild(document.createTextNode("..."));
		var link = document.createElement("a");
		link.href = "#";
		var menuid = getMenu(type);
		addEvent(link,"click", function() { loadPage(url,menuid); return false;});
		link.innerHTML = "more";
		news.appendChild(link);
	}
	
	section.appendChild(news);
	if (divider) {
		var divider = document.createElement("div");
		divider.className = "divider";
		section.appendChild(divider);
	}
}

function getMenu(type) {
	if (type == "A") return "Articles";
	else if (type == "C") return "Calculators";
	else if (type == "E") return "Events";
	else if (type == "T") return "Tank_Gallery";
	else if (type == "M") return "Miscellaneous";
	else if (type == "D") return "DIY";
	else if (type == "R") return "Reviews";
	else if (type == "P") return "Plants";
	else return "Home";
}

function resetNewsPagination(pageNo,totalPages) {
	//reset the nav section
	var nav = document.getElementById("news-nav");
	nav.innerHTML = "";
	//print the next button
	var elem = document.createElement("span");
	if (totalPages > pageNo) {
		addEvent(elem,"click", function() { Paginator.go(pageNo + 1); return false;});
		elem.setAttribute("onclick", "Paginator.go(" + (pageNo + 1) + "); return false;");
		elem.className = "nav-button";
	} else {
		elem.className = "nav-button-inactive";
	}
	elem.appendChild(document.createTextNode(">"));
	nav.appendChild(elem);
	//print the prev button
	elem = document.createElement("span");
	if (pageNo > 1) {
		addEvent(elem,"click", function() { Paginator.go(pageNo - 1); return false;});
		elem.setAttribute("onclick", "Paginator.go(" + (pageNo - 1) + "); return false;");
		elem.className = "nav-button";
	} else {
		elem.className = "nav-button-inactive";
	}
	elem.appendChild(document.createTextNode("<"));
	nav.appendChild(elem);
	nav.style.visibility = "visible";
}

var Artifact = new Object();
Artifact.printList = function(id,title,section) {
	var option = document.createElement("option");
	option.setAttribute("value", id);
	option.appendChild(document.createTextNode(title));
	section.appendChild(option);
}
Artifact.print = function(id,type,title,description,url,dtTime,count,title_img,section) {
	var artifactUrl = "./util/artifact_template.php&pg=" + url + "&artifactType=" + type + "&artifactId=" + id + "&artifactTitle=" + title + "&pageNo=" + Paginator.pageNo;
	var menuid = getMenu(type);
	//add the header
	var elem = document.createElement("div");
	elem.className = "artifact-body-section";
	section.appendChild(elem);
	var child = document.createElement("p");
	child.setAttribute("align","left");
	elem.appendChild(child);
	//add the title
	var child1 = document.createElement("div");
	child1.className = "artifact-title";
	addEvent(child1,"click", function() { window.parent.loadPage(artifactUrl,null)});
	child1.innerHTML = title;
	child.appendChild(child1);
	//add the date & time
	child1 = document.createElement("div");
	child1.className = "artifact-dttime";
	child1.innerHTML = "Updated on " + dtTime + " (" + count + " visits)";
	child.appendChild(child1);
	//add the body
	elem = document.createElement("div");
	elem.className = "artifact-body-section";
	section.appendChild(elem);
	//add the description
	child = document.createElement("p");
	child.setAttribute("align","left");
	//add the title image
	var lytebox = document.createElement("a");
	lytebox.setAttribute("href",title_img);
	lytebox.setAttribute("rel", "lytebox[" + type + "]");
	lytebox.setAttribute("title", "");
	var img = "<img style='max-width: 150px' height='70px' class='zoomable' align='right' border='0' src='" + title_img + "'>";
	lytebox.innerHTML = img;
	child.appendChild(lytebox);
	child.appendChild(document.createTextNode(description));
	elem.appendChild(child);
}

function showArtifact() {
	document.getElementById("artifact-comment-body").className = "sub-section-inactive";
	document.getElementById("artifact-body").className = "sub-section-active";
	document.getElementById("artifact-comment-tab").className = "comment-link clickable left sub-section-active";
	document.getElementById("artifact-tab").className = "sub-section-inactive clickable left";
	backToArtifact();
	parent.resizeIframe();
}

function showArtifactComments() {
	document.getElementById("artifact-body").className = "sub-section-inactive";
	document.getElementById("artifact-comment-body").className = "sub-section-active";
	document.getElementById("artifact-tab").className = "sub-section-active clickable left";
	document.getElementById("artifact-comment-tab").className = "comment-link clickable left sub-section-inactive";
	parent.resizeIframe();
}

function showBrowserStats(e) {
	 var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  var evt=(!e)?window.event:e;
	  document.getElementById("browser-info").style.left = (scrOfX + evt.clientX) + "px";
	  document.getElementById("browser-info").style.top = (scrOfY + evt.clientY) + "px";
	  document.getElementById("browser-info").style.display="block";
	
	  return false;
}

function resizeIframe(){
	var currentfr=document.getElementById("content_frame");
	if (currentfr) {
		if (!window.opera){
			currentfr.style.display="block";
		}
		var width = (window.size()["width"]*0.95);
		if (width < 820) width = 820;
		document.getElementById("main_content").style.width = width + "px";
		document.getElementById("footer-section").style.width = width + "px";
		document.getElementById("footer-divider-center").style.width=(width-201)+"px";
		currentfr.style.height="0px";
		width = width - 212;
		if (width < 607){width = 607;}
		currentfr.style.width = width + "px";
		content_frame.document.getElementById("body_content").style.width = (width - 172) + "px";
		if (currentfr.contentDocument && currentfr.contentDocument.body.scrollHeight) {
			currentfr.style.height= currentfr.contentDocument.body.scrollHeight+10 + "px";
		} else if (currentfr.Document.body.scrollHeight) {
			currentfr.style.height= currentfr.Document.body.scrollHeight+10 + "px";
		}
		currentfr.style.visibility = "visible";
	}
}

function repaintAdmin() {
	var width = (window.size()["width"]*0.95);
	document.getElementById("admin-header-section").style.width = width + "px";
	document.getElementById("admin-divider-center").style.width=(width-201)+"px";
}

function showAdmin(section) {
	document.getElementById("form-msg").innerHTML = "";
	document.getElementById(currentSection).className = "sub-section-inactive";
	if (document.getElementById((currentSection) + "-tab").className == "tab-active-left")
		document.getElementById(currentSection + "-tab").className = "tab-inactive-left";
	else
		document.getElementById(currentSection + "-tab").className = "tab-inactive";
	document.getElementById(section).className = "sub-section-active";
	if (document.getElementById((section) + "-tab").className == "tab-inactive-left")
		document.getElementById(section + "-tab").className = "tab-active-left";
	else
		document.getElementById(section + "-tab").className = "tab-active";
	currentSection = section;
	plantIdList = null;
	if (section == "news") { 
		fbPublishCallback = cleanupNews;
		document.getElementById("artifact").innerHTML = "";
		Ajax.call("GET",webRoot + "/php/admin/news.php",null,adminSubsectionLoadCallback,false);
	} else if (section == "artifact") {
		fbPublishCallback = showArtifactAdmin;
		plantIdList = "a-plant-id";
		newPlant = false;
		document.getElementById("news").innerHTML = "";
		Ajax.call("GET",webRoot + "/php/admin/artifacts.php",null,adminSubsectionLoadCallback,false);
	} else if (section == "plants") {
		plantIdList = "plant-id";
		newPlant = true;
		loadPlants();
	}
}

var Email = new Object();
Email.print = function(id,ip,name,replyTo,subject,msg,dtTime,read,section) {
	var row = document.createElement("tr");
	row.setAttribute("id", "email" + id);
	if (parseInt(read) == 0) row.className = "bold";
	else row.className = "normal";
	section.appendChild(row);
	
	var cell = document.createElement("td");
	cell.setAttribute("align","center");
	cell.className = "table-body table-body-leftmost";
	row.appendChild(cell);
	
	var child = document.createElement("input");
	child.setAttribute("type", "checkbox");
	child.setAttribute("name", "emailid");
	child.setAttribute("value", id);
	cell.appendChild(child);
	
	cell = document.createElement("td");
	cell.setAttribute("align","center");
	cell.className = "table-body";
	cell.innerHTML = name + " &lt;" + replyTo + "&gt;";
	row.appendChild(cell);

	cell = document.createElement("td");
	cell.setAttribute("align","center");
	cell.className = "table-body ";
	cell.innerHTML = dtTime;
	row.appendChild(cell);

	cell = document.createElement("td");
	cell.setAttribute("align","center");
	cell.className = "table-body";
	cell.innerHTML = subject;
	row.appendChild(cell);

	cell = document.createElement("td");
	cell.setAttribute("align","center");
	cell.className = "table-body";
	cell.innerHTML = "<img src='../../images/collapsed.gif' class='clickable' onclick='showMsg(" + id + ",this," + read + ")' border='0'>";
	row.appendChild(cell);
	row = document.createElement("tr");
	row.setAttribute("id", "msgbody" + id);
	section.appendChild(row);
	
	cell = document.createElement("td");
	cell.setAttribute("colspan","5");
	cell.className = "msg-body table-body-leftmost";
	cell.innerHTML = msg;
	row.appendChild(cell);
	row.style.display="none";
}

function showMsg(msgId,button,read) {
	var section = document.getElementById("msgbody" + msgId);
	if (section.style.display == 'none') {
		button.src = "../../images/expanded.gif";
		section.style.display = "table-row";
		if (read == 0) {
			document.getElementById("email" + msgId).className = "normal";
			button.setAttribute("onclick","showMsg(" + msgId + ",this,1)");
			document.getElementById("func").value = "M";
			Ajax.call("POST",document.forms[0].action,"func=M&email_id=" + msgId ,inboxUpdatePostCallback,false);
		}
	} else {
		button.src = "../../images/collapsed.gif";
		section.style.display = "none";
	}
}

var Plant = new Object();
Plant.print = function(id,name,section) {
	var option = document.createElement("option");
	option.setAttribute("value", id);
	option.appendChild(document.createTextNode(name));
	section.appendChild(option);
}

function cleanupNews() {
	document.getElementById("news-type").selectedIndex = 3;
	document.getElementById("news-text").value = "";
	document.getElementById("url").value = "";
}

function showArtifactAdmin() {
	Ajax.call("GET",webRoot + "/php/admin/artifacts.php",null,adminSubsectionLoadCallback,false);
}

function showCalc(section) {
	document.getElementById("form-msg").innerHTML = "";
	document.getElementById(currentCalc).className = "sub-section-inactive";
	document.getElementById("calc-comments").className = "sub-section-inactive";
	document.getElementById("calc-tab").className = "sub-section-inactive clickable";
	if (document.getElementById((currentCalc) + "-tab").className == "tab-active-left")
		document.getElementById(currentCalc + "-tab").className = "tab-inactive-left";
	else
		document.getElementById(currentCalc + "-tab").className = "tab-inactive";
	document.getElementById(section).className = "sub-section-active";
	if (document.getElementById((section) + "-tab").className == "tab-inactive-left")
		document.getElementById(section + "-tab").className = "tab-active-left";
	else
		document.getElementById(section + "-tab").className = "tab-active";
	currentCalc = section;
	resetCalcId();
	calcCommentCountUpdate();
	parent.resizeIframe();
}

function showCurrentCalc() {
	document.getElementById("form-msg").innerHTML = "";
	document.getElementById("calc-comments").className = "sub-section-inactive";
	document.getElementById(currentCalc).className = "sub-section-active";
	document.getElementById("calc-tab").className = "sub-section-inactive clickable";
	calcCommentCountUpdate();
	parent.resizeIframe();
}

function showCalcComments(artifactType,artifactId,artifactName) {
	document.getElementById("form-msg").innerHTML = "";
	document.getElementById(currentCalc).className = "sub-section-inactive";
	document.getElementById("calc-comments").className = "sub-section-active";
	document.getElementById("calc-tab").className = "sub-section-active clickable";
	reloadCalcComments(artifactType,artifactId,artifactName);
	parent.resizeIframe();
}

function showSubCalc(section,calcType) {
	document.getElementById("form-msg").innerHTML = "";
	document.getElementById("calc-comments").className = "sub-section-inactive";
	document.getElementById("calc-tab").className = "sub-section-inactive clickable";
	var subCalc;
	if (calcType == 'C') subCalc = currentConcentrationCalc;
	document.getElementById(subCalc).className = "sub-section-inactive";
	if (document.getElementById((subCalc) + "-tab").className == "tab-active-left")
		document.getElementById(subCalc + "-tab").className = "tab-inactive-left";
	else
		document.getElementById(subCalc + "-tab").className = "tab-inactive";
	document.getElementById(section).className = "sub-section-active";
	if (document.getElementById((section) + "-tab").className == "tab-inactive-left")
		document.getElementById(section + "-tab").className = "tab-active-left";
	else
		document.getElementById(section + "-tab").className = "tab-active";
	if (calcType == 'C') currentConcentrationCalc = section;
	resetCalcId();
	calcCommentCountUpdate();
	parent.resizeIframe();
}

function resetCalcId() {
	if (currentCalc == "volume") calcId = 1;
	else if (currentCalc == "concentration") {
		if (currentConcentrationCalc == "co2") calcId = 2;
		else calcId = 3;
	}
	else if (currentCalc == "dosing") calcId = 4;
	else if (currentCalc == "electricity") calcId = 5;
}

function showTank(type) {
	var tankPic = document.getElementById("tank-pic");
	var source;
	if (type.value == "") {
		tankPic.style.display = "none";
		dimensionFieldDisplay(false,type.value);
	} else {
		if (type.value == "BO") source = "../images/calc/bowfront.jpg";
		else if (type.value == "BO") source = "../images/calc/bowfront.jpg";
		else if (type.value == "CI") source = "../images/calc/circle.jpg";
		else if (type.value == "CO") source = "../images/calc/concave.jpg";
		else if (type.value == "HE") source = "../images/calc/hexagon.jpg";
		else if (type.value == "OC") source = "../images/calc/octagon.jpg";
		else if (type.value == "RE") source = "../images/calc/rectangle.jpg";
		else if (type.value == "SC") source = "../images/calc/semicircular.jpg";
		tankPic.src = source;
		document.getElementById("tank-pic-link").href = source;
		tankPic.style.display = "inline";
		dimensionFieldDisplay(true,type.value);
	}
	parent.resizeIframe();
}

function dimensionFieldDisplay(display,type) {
	if (display) {
		if (type == "CI") {
			document.getElementById("tank-length-label").innerHTML = "Outer Diameter (D):[Required]";
			document.getElementById("tank-length-help").title = "Refer to 'D' on diagram";
		} else if (type == "HE" || type == "OC") {
			document.getElementById("tank-length-label").innerHTML = "Outer Length of each side (L):[Required]";
			document.getElementById("tank-length-help").title = "Refer to 'L' on diagram";
		} else {
			document.getElementById("tank-length-label").innerHTML = "Outer Length (L):[Required]";
			document.getElementById("tank-length-help").title = "Refer to 'L' on diagram";
		}

		if (type == "BO" || type == "CO" || type == "RE") {
			document.getElementById("tank-width-label").innerHTML = "Outer Width (W):[Required]";
			document.getElementById("W").disabled = false;

			if (type == "BO" || type == "CO") {
				document.getElementById("tank-cwidth-label").innerHTML = "Center Width (Wc):[Required]";
				document.getElementById("Wc").disabled = false;
			} else {
				document.getElementById("tank-cwidth-label").innerHTML = "Center Width (Wc):";
				document.getElementById("Wc").disabled = true;
			}
		} else {
			document.getElementById("tank-width-label").innerHTML = "Outer Width (W):";
			document.getElementById("tank-cwidth-label").innerHTML = "Center Width (Wc):";
			document.getElementById("W").disabled = true;
			document.getElementById("Wc").disabled = true;
		}
	} else {
		document.getElementById("tank-length-label").innerHTML = "";
		document.getElementById("tank-length-help").title = "Select tank type to see help";
		document.getElementById("tank-width-label").innerHTML = "Outer Width (W):";
		document.getElementById("tank-cwidth-label").innerHTML = "Center Width (Wc):";
		document.getElementById("W").disabled = true;
		document.getElementById("Wc").disabled = true;
	}
}